<script setup>
  import dayjs from 'dayjs';
  import { useAuthStore } from '@/stores/auth';
  import { runtimeIsEndTask } from '@/api/flow/runtime';
  import { useIntersectionObserver } from '@vueuse/core';
  import { storeToRefs } from 'pinia';
  const { userInfo } = storeToRefs(useAuthStore());
  const attrs = useAttrs();
  const emits = defineEmits(['submit']);
  const props = defineProps({
    // 是否显示审核结果字段(不显示就是默认就是通过)
    showIdea: {
      type: Boolean,
      default: true
    },
    showSave: {
      type: Boolean,
      default: false
    },
    // 是否显示审核意见字段
    showIdeaSuggest: {
      type: Boolean,
      default: true
    },
    labelOptions: {
      type: Object,
      default: () => ({
        ideaSuggest: '审核意见',
        idea: '审核结果',
        assigneeName: '审核人',
        time: '审核时间'
      })
    },
    /**
     * /flow/runtime/isEndTask接口的请求参数 ,必传以下三个字段
     */
    isEndTaskParams: {
      type: Object,
      default: () => ({
        flowKey: 'fxyj',
        taskId: 'toTask2',
        bizKey: '1550861039829024'
      }),
      required: true
    }
  });
  const auditFormRef = ref();
  const auditFormData = reactive({
    ideaSuggest: '',
    idea: '1',
    ideaMsg: '通过',
    assigneeName: userInfo.value.realName,
    assignee: userInfo.value.id,
    time: dayjs().format('YYYY-MM-DD HH:mm:ss')
  });
  const rules = {
    ideaSuggest: [{ required: true, message: `请输入${props.labelOptions.ideaSuggest}` }],
    idea: [{ required: true, message: `请输入${props.labelOptions.idea}` }]
  };
  // 如果这个审核表单从不可见变成可见 就更新一下时间
  watchEffect(() => {
    useIntersectionObserver(auditFormRef, ([{ isIntersecting }]) => {
      if (isIntersecting) {
        auditFormData.time = dayjs().format('YYYY-MM-DD HH:mm:ss');
      }
    });
  });

  defineExpose({
    auditFormRef,
    auditFormData,
    handleResetForm
  });
  function handleResetForm() {
    nextTick(() => {
      auditFormRef.value.resetFields();
    });
  }
  function handleChange(value) {
    auditFormData.ideaMsg = value === '1' ? '通过' : '不通过';
  }
  function handleSubmit() {
    auditFormRef.value.validate(async (valid) => {
      if (!valid) {
        return NstdMessage.error('存在必填项未填写');
      }
      if (String(auditFormData.idea) === '0') {
        // 默认下一步是结束的
        emits('submit', auditFormData, true);
      } else {
        try {
          const { data: isEnd } = await runtimeIsEndTask({ ...props.isEndTaskParams, idea: auditFormData.idea });
          emits('submit', auditFormData, isEnd);
        } catch (error) {
          console.error(error, 'runtimeIsEndTask');
          // 默认下一步是结束的
          emits('submit', auditFormData, true);
        }
      }
    });
  }
</script>

<template>
  <NstdForm v-bind="attrs" :ref="(el) => (auditFormRef = el)" :rules="rules" :model="auditFormData">
    <NstdRow :gutter="20">
      <NstdCol :span="24" v-if="props.showIdeaSuggest">
        <NstdFormItem :label="props.labelOptions.ideaSuggest" prop="ideaSuggest">
          <NstdInput type="textarea" v-model="auditFormData.ideaSuggest" />
        </NstdFormItem>
      </NstdCol>
      <NstdCol :span="24" v-if="props.showIdea">
        <NstdFormItem :label="props.labelOptions.idea" prop="idea">
          <NstdRadioGroup @change="handleChange" v-model="auditFormData.idea">
            <NstdRadio label="1">通过</NstdRadio>
            <NstdRadio label="0">不通过</NstdRadio>
          </NstdRadioGroup>
        </NstdFormItem>
      </NstdCol>
      <NstdCol :span="8">
        <NstdFormItem :label="props.labelOptions.assigneeName" prop="assigneeName">
          <NstdInput v-model="auditFormData.assigneeName" disabled />
        </NstdFormItem>
      </NstdCol>
      <NstdCol :span="8">
        <NstdFormItem :label="props.labelOptions.time">
          <NstdDatePicker disabled type="datetime" value-format="YYYY-MM-DD HH:mm:ss" v-model="auditFormData.time" />
        </NstdFormItem>
      </NstdCol>
      <NstdCol :span="8">
        <NstdButton type="primary" size="small" @click="handleSubmit">提交</NstdButton>
        <NstdButton v-if="props.showSave" type="primary" size="small" @click="handleSubmit">保存</NstdButton>
      </NstdCol>
    </NstdRow>
  </NstdForm>
</template>

<style scoped lang="scss"></style>
